package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.UserException;
import com.itheima.pojo.Menu;
import com.itheima.service.TestUserService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    UserService userService;

    @RequestMapping("/getUserName")
    public Result getUserName(){

//        org.springframework.security.core.userdetails.User user =
//                (org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return Result.success("", "");

    }

    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        List<Menu> menus = userService.findAllMenu();
        return Result.success("",menus);
    }

    @RequestMapping("/findRightPage")
    public PageResult findRightPage(@RequestBody QueryPageBean queryPageBean){
        return userService.findRightPage(queryPageBean);
    }



    @RequestMapping("/doAdd")
    public Result doAdd(@RequestBody Menu menu) {
        try {
            userService.doAdd(menu);
            return Result.success("添加成功");
        } catch (UserException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
    }

    @RequestMapping("/remove")
    public Result remove(Integer id) {
        try {
            userService.remove(id);
            return Result.success("删除菜单成功");
        } catch (UserException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Menu menu = userService.findById(id);
        return Result.success("", menu);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            userService.edit(menu);
            return Result.success("编辑菜单成功");
        } catch (UserException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            return Result.error("编辑菜单失败");
        }
    }

}
